package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.zjitc.entity.Manager;
import net.zjitc.entity.User;
import net.zjitc.entity.Yonghu;
import net.zjitc.mapper.ManagerMapper;
import net.zjitc.mapper.UserMapper;
import net.zjitc.service.IManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @auther 郭梦婷
 * @create 2022-05-25 11:37:55
 * @describe 服务实现类
 */
@Service
@Transactional
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements IManagerService {

    @Autowired
    private ManagerMapper managerMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Manager selectByUsername(String username) {
        QueryWrapper<Manager> wrapper=new QueryWrapper<>();
        wrapper.eq("mgusername",username);
        Manager manager = managerMapper.selectOne(wrapper);
        return manager;


    }

    @Override
    public Page<Manager> getManagers(String querystring, Integer pagenum, Integer pagesize) {
        Page<Manager> page=new Page<>(pagenum,pagesize);
        if (querystring.length()==0){
            page = managerMapper.selectPage(page,null);
        }else {
            QueryWrapper<Manager> wrapper=new QueryWrapper<>();
            wrapper.like("mgusername",querystring).or().like("mgname",querystring);
            page = managerMapper.selectPage(page, wrapper);
        }
        return page;
    }

    @Override
    public int deleteById(int id) {
        Manager manager = managerMapper.selectById(id);
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("username",manager.getMgusername());
        userMapper.delete(wrapper);
        int i = managerMapper.deleteById(id);
        return i;
    }
}
